package leetcode.stack;

import org.junit.Test;

import java.util.*;

public class work {


    @Test
    public void findNumberIn2DArray( ) {
        int[][] matrix = {{}};
        int[][] matrix1 = {
                          {1,   4,  7, 11, 15},
                          {2,   5,  8, 12, 19},
                          {3,   6,  9, 16, 22},
                          {10, 13, 14, 17, 24},
                          {18, 21, 23, 26, 30}
                        };
        int target = 5;

        if(matrix.length == 1){
            if(matrix[0].length < 1){

                System.out.println("2");
            }
        }else{
            for (int i = 0; i < matrix.length; i++) {
                if(matrix[i][0]>target){
                    System.out.println(false);
                }
                for (int j = 0; j < matrix[0].length; j++) {
                    if (matrix[i][j]>target){
                        break;
                    }
                    if(matrix[i][j] == target){
                        System.out.println(true);
                    }
                }
            }
            System.out.println(false);

        }











    }





















    @Test
    public void firstUniqChar() {

        String s = "abaccdeff";
        Map<Character, Integer> map = new LinkedHashMap<>();

        for (int i = 0; i < s.length(); i++) {
            char one = s.charAt(i);
            if(map.get(one)==null){
                map.put(one,1);
            }else {
                map.put(one, map.get(one) + 1);
            }
        }
        Set<Character> set = map.keySet();
        for(Character a:set){
            if(map.get(a)==1){
                System.out.println(a);
            }
        }




    }













    @Test
    public void missingNumber() {
        int[] nums = {0,1};




        for(int i =0; i<nums.length;i++){
            if(nums[i] != i){
                System.out.println(i);
            }
        }
        System.out.println(nums.length);

    }


    @Test
    public void findRepeatNumber() {
        int[] nums = {2, 3, 1, 0, 2, 5, 3};
        int target = 2;

        Set<Integer> set = new LinkedHashSet<>();
        int time = 0;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] == target){
                time++;
            }
        }
        System.out.println(time);
    }






    Map<Node, Node> cachedNode = new HashMap<Node, Node>();
    @Test
    public void reverseLeftWords() {
        String s = "abcdefg";
        int k = 2;

        int len = s.length();
        char cur;
        char[] list = new char[len];
        for (int i = k; i < len; i++) {
            cur = s.charAt(i);
            list[i-k] = cur;
        }

        for (int i = 0; i < k; i++) {
            cur = s.charAt(i);
            list[len-k+i] = cur;
        }

        String l = new String(list, 0, len);
        System.out.println(l);


    }




    @Test
    public void replaceSpace() {
        String s = "We are happy.";
        int len = s.length();
        char[] chars = new char[len*3];
        int size = 0;
        char cur;
        for (int i = 0; i < len; i++) {
            cur = s.charAt(i);
            if(cur == ' '){
                chars[size++] = '%';
                chars[size++] = '2';
                chars[size++] = '0';
            }else {
                chars[size++] = cur;
            }
        }
        String list = new String(chars,0,size);
        System.out.println(list);
    }


//        System.out.println(s.split(" "));
//        while(len>=i+1){
//
//            if (s.substring(i,i+1) == (" ")) {
//                System.out.println(123);
//            }
//            i++;
//        }











//    @Test
//    public void copyRandomList(Node head) {
//        if (head == null) {
//            return null;
//        }
//        if (!cachedNode.containsKey(head)) {
//            Node headNew = new Node(head.val);
//            cachedNode.put(head, headNew);
//            headNew.next = copyRandomList(head.next);
//            headNew.random = copyRandomList(head.random);
//        }
//        cachedNode.get(head);
//        System.out.println();
//    }





@Test
    public void reversePrint() {
        ListNode head = new ListNode(1);
        head.next = new ListNode(3);
        head.next.next = new ListNode(2);
        List<Integer> list = new ArrayList<>();
        while(head!=null){
            list.add(head.val);
            head = head.next;
        }
        System.out.println(list.size());
        int[] arr = new int[list.size()];
        System.out.println(list.get(0));
        System.out.println(list.get(1));
        System.out.println(list.get(2));
        int i = 0;
        while(i<list.size()){
            arr[i] = list.get(0);
            System.out.println(arr[i]);
            i++;
        }



    }

    @Test
    public void min(){
//        Stack<Integer> minStack = new Stack<>();

        MinStack minStack = new MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        System.out.println(minStack.min());
        minStack.pop();
        minStack.pop();
        System.out.println(minStack.top());
        System.out.println(minStack.min());


    }
}
class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}
class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}